package org.rhq.plugins.meminfo;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Set;

import org.hyperic.sigar.Mem;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.measurement.MeasurementDataNumeric;
import org.rhq.core.domain.measurement.MeasurementDataTrait;
import org.rhq.core.domain.measurement.MeasurementReport;
import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ResourceComponent;
import org.rhq.core.pluginapi.inventory.ResourceContext;
import org.rhq.core.pluginapi.measurement.MeasurementFacet;

public class MemoryServiceComponent implements ResourceComponent, MeasurementFacet {

    private Mem mem;

    /* (non-Javadoc)
     * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
     */
    public AvailabilityType getAvailability() {
        return AvailabilityType.UP;
    }

    /* (non-Javadoc)
     * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext)
     */
    public void start(ResourceContext context) throws InvalidPluginConfigurationException, Exception {
        mem = context.getSystemInformation().getMemoryInfo();
    }

    /* (non-Javadoc)
     * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop()
     */
    public void stop() {
        // nothing to do here

    }

    public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {

        getData();
        for (MeasurementScheduleRequest request : metrics) {
            if (request.getName().equals("total")) {
                report.addData(new MeasurementDataTrait(request, String.valueOf(mem.getTotal())));
            } else if (request.getName().equals("free")) {
                report.addData(new MeasurementDataNumeric(request, new Double(mem.getFree())));
            }
        }
    }

    private void getData() {
        // do nothing
    }

}
